<?php              
define('DEFAULT_TABLE_ROWS', 1);

$var_cui_furnizor = 0;
$var_id_program = 0;
$var_id_sesiune = 0;

if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['cui_furnizor']) and isset($_POST['id_program']))
{
	$var_cui_furnizor = $_POST['cui_furnizor'];
	$var_id_program = $_POST['id_program'];
}
else  if($_SERVER['REQUEST_METHOD'] == "GET" and isset($_GET['cui_furnizor']) and isset($_GET['id_program']))
{
	$var_cui_furnizor = $_GET['cui_furnizor'];
	$var_id_program = $_GET['id_program'];
}


if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['id_program']))
{
	$var_id_program = $_POST['id_program'];
}
else if($_SERVER['REQUEST_METHOD'] == "GET" and isset($_GET['id_program']))
{
	$var_id_program = $_GET['id_program'];
}

if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['id_sesiune']))
{
	$var_id_sesiune = $_POST['id_sesiune'];
}
else if($_SERVER['REQUEST_METHOD'] == "GET" and isset($_GET['id_sesiune']))
{
	$var_id_sesiune = $_GET['id_sesiune'];
}

include "functions".DIRECTORY_SEPARATOR."utils.php";
include "includes".DIRECTORY_SEPARATOR."include_user_sessions_forms_for_tables.php";
							
$form_fields_with_speficications = array();

for($index = 1; $index <= DEFAULT_TABLE_ROWS; $index+=1) 
{
	foreach( $form_for_table as $form_column_table )
	{
		array_push( $form_fields_with_speficications,$form_column_table);
	}
}
	
foreach($form_fields_with_speficications as $form_key => $form_value)
{
	if( ! isset($form_fields_with_speficications[$form_key]['colors']) )
			$form_fields_with_speficications[$form_key]['colors'] = array('first' => '#333333', 'second' => 'red');

	if( ! isset($form_fields_with_speficications[$form_key]['field_size']) )
			$form_fields_with_speficications[$form_key]['field_size'] = null;
	if( ! isset($form_fields_with_speficications[$form_key]['max_length']) )
			$form_fields_with_speficications[$form_key]['max_length'] = null;
	if( ! isset($form_fields_with_speficications[$form_key]['regex']) )
			$form_fields_with_speficications[$form_key]['colors'] = null;						  
	if( ! isset($form_fields_with_speficications[$form_key]['can_be_null']) )
			$form_fields_with_speficications[$form_key]['can_be_null'] =  false;
	
	
	if( ! isset($form_fields_with_speficications[$form_key]['current_value']))
	{
		if( $form_fields_with_speficications[$form_key]['field_type'] == 'text')
		{
			if( isset($form_fields_with_speficications[$form_key]['initial_values']) )
				$form_fields_with_speficications[$form_key]['current_value'] = $form_fields_with_speficications[$form_key]['initial_values'];
			else
				$form_fields_with_speficications[$form_key]['current_value'] =  '';
		}
		else if( $form_fields_with_speficications[$form_key]['field_type'] == 'drop_down_list')
		{
			$form_fields_with_speficications[$form_key]['current_value'] = "neselectat";
		}
	}
	
	$form_fields_with_speficications[$form_key]['current_color'] = $form_fields_with_speficications[$form_key]['colors']['first'];	
}

$form_content = generate_form_content($form_fields_with_speficications);

$erori = "";


if ( isset($_GET['add_sesiune']) or isset($_POST['add_sesiune']) )
{
$get_form=<<<GATA
	<form method="post" action = "user_commission_search.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="add_sesiune" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<input type="hidden" name="id_program" value="$var_id_program"> 
			<tr><td></td>
				<td><input type ="submit" value = "Salveaza" ></td>
			</tr>
		</table>
	</form>
GATA;
}
else if ( isset($_GET['edit_sesiune']) or isset($_POST['edit_sesiune']) )
{
$get_form=<<<GATA
	<form method="post" action = "user_commission_search.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="edit_sesiune" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<input type="hidden" name="id_program" value="$var_id_program"> 
			<input type="hidden" name="id_sesiune" value="$var_id_sesiune"> 
			<tr><td></td>
				<td><input type ="submit" value = "Salveaza" ></td>
			</tr>
		</table>
	</form>
GATA;
}
else if ( isset($_GET['view_sesiune']) or isset($_POST['view_sesiune']) )
{
$get_form=<<<GATA
	<form method="post" action = "user_commission_search.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="view_sesiune" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<input type="hidden" name="id_program" value="$var_id_program"> 
			<input type="hidden" name="id_sesiune" value="$var_id_sesiune"> 
			<tr><td></td>
				<td><input type ="submit" value = "Salveaza" ></td>
			</tr>
		</table>
	</form>
GATA;
}
else
{
$get_form=<<<GATA
	<form method="post" action = "user_commission_search.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<tr><td></td>
				<td><input type ="submit" value = "Salveaza" ></td>
			</tr>
		</table>
	</form>
GATA;
}

function has_value($input_name){
	if ( isset($_POST[$input_name]) && !empty($_POST[$input_name]))
		return true;
	else 
		return false;
}

function get_value($input_name){
	if ( isset($_POST[$input_name]) && !empty($_POST[$input_name]))
		return $_POST[$input_name];
	else 
		return NULL;
}

if($_SERVER['REQUEST_METHOD'] == 'GET')
{
	echo $get_form;
}


if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	
$ok = 1;
	foreach($form_fields_with_speficications as $form_key => $form_value)
	{
	if( isset($_POST[$form_fields_with_speficications[$form_key]['post_name']]))
	{									
		if(empty($_POST[$form_fields_with_speficications[$form_key]['post_name']]) 
		|| ( $_POST[$form_fields_with_speficications[$form_key]['post_name']] == 'neselectat') )
		//if (( $_POST[$form_fields_with_speficications[$form_key]['post_name']] == 'neselectat'))
		{
			# Something went wrong with this form element so an error will be displayed to user
			$ok = 0;
			
			# Change color for element ; default will be red
			$form_fields_with_speficications[$form_key]['current_color'] = "red";
			if( isset($form_fields_with_speficications[$form_key]['colors'] ))
			{
				if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
				{
					$form_fields_with_speficications[$form_key]['current_color'] = 
					$form_fields_with_speficications[$form_key]['colors']['second'];		
				}
			}
			
			# Generate an error if nothing it's provided
			$default_generated_error = "";
			
			if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
			{
				$default_generated_error = 
				"Nu a fost introdus nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
			}
			else if( $form_fields_with_speficications[$form_key]['field_type'] == "drop_down_list" )
			{
				$default_generated_error =
				 "Nu a fost selectat nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
			}
			else
			{
				$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['post_name']." este gol</br>";
			}
			
			# Add the provided error or the default one
			if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
			{
				if( isset($form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'] ))
				{
					$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'];					
				}
				else
				{
					$erori = $erori.$default_generated_error;
				}
			}
			else
			{
				$erori = $erori.$default_generated_error;
			}
			
			continue;
		}
		
		# Check regex here
		if ( isset( $form_fields_with_speficications[$form_key]['regex'] ) )
		{
			if ( ! empty( $form_fields_with_speficications[$form_key]['regex'] ) )
			{
				if( preg_match($form_fields_with_speficications[$form_key]['regex'],
				$_POST[$form_fields_with_speficications[$form_key]['post_name']])== 0 )
				{																		
								
					# Something went wrong with this form element so an error will be displayed to user
					$ok = 0;
					
					# Change color for element ; default will be red
					$form_fields_with_speficications[$form_key]['current_color'] = "red";
					if( isset($form_fields_with_speficications[$form_key]['colors'] ))
					{
						if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
						{
							$form_fields_with_speficications[$form_key]['current_color'] = 
							$form_fields_with_speficications[$form_key]['colors']['second'];		
						}
					}
					
					# Generate an error if nothing it's provided
					$default_generated_error = "";
					
					if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
					{
						$default_generated_error = "Datele introduse pentru campul ".$form_fields_with_speficications[$form_key]['field_display_name']."sunt invalide</br>";
					}
					else
					{
						$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['field_display_name']." este invalid</br>";
					}
					
					# Add the provided error or the default one
					if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
					{
						if( isset($form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'] ))
						{
							$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'];					
						}
						else
						{
							$erori = $erori.$default_generated_error;
						}
					}
					else
					{
						$erori = $erori.$default_generated_error;
					}
				
					continue;
				}
			}
		}
		
		$form_fields_with_speficications[$form_key]['current_value'] = $_POST[$form_fields_with_speficications[$form_key]['post_name']];  											
	}
}

	$mail_address = "-";
	if ( defined('MAIL') )
	{
		$mail_address = MAIL;
	}

if($ok != 1)
{
	$form_content = generate_form_content($form_fields_with_speficications);

if ( isset($_GET['add_sesiune']) or isset($_POST['add_sesiune']) )
{
	$form_post=<<<GATA
		<form method="post" action = "user_commission_search.php">
			<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="add_sesiune" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<input type="hidden" name="id_program" value="$var_id_program"> 
			<tr>
				<td></td><td><input type ="submit" value = "Salveaza" onclick="this.form.target='_self';return true;" name="comanda"></td>
			</tr>
	</table>
	</form>
GATA;
}
else if ( isset($_GET['edit_sesiune']) or isset($_POST['edit_sesiune']) )
{
	$form_post=<<<GATA
		<form method="post" action = "user_commission_search.php">
			<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="edit_sesiune" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<input type="hidden" name="id_program" value="$var_id_program">
			<input type="hidden" name="id_sesiune" value="$var_id_sesiune"> 
			<tr>
				<td></td><td><input type ="submit" value = "Salveaza" onclick="this.form.target='_self';return true;" name="comanda"></td>
			</tr>
	</table>
	</form>
GATA;
}


	echo $form_post;
	echo "<font color=\"red\">".$erori."</font>";
}
else  
{
	require_once ('mysql_connect.php');
	
	if ( isset($_GET['add_sesiune']) or isset($_POST['add_sesiune']) )
	{
		$u = mysqli_query($dbc,"INSERT INTO `sesiuni`
			(`id_furnizori_programe`,`data_sesiunii`, `nr_persoane_inscrise`,`nr_persoane_finalizat`,
			`nr_participanti_absolvire`, `nr_absolventi_barbati_sub_25`,`nr_absolventi_barbati_peste_25`,
			`nr_asolventi_femei_sub_25`, `nr_absolventi_femei_peste_25`,`nr_absolventi_alte_sesiuni`,
			`desfasurat_in_alt_judet`,`observatii`,`email_user_comisie`)
			 VALUES 
			 ('$var_id_program','$_POST[session_date]', '$_POST[session_subscriber_nr]',  								
			 '$_POST[session_finalists]','$_POST[exam_participants]',   																				         '$_POST[men_graduates_less_25]','$_POST[men_graduates_more_25]',
			 '$_POST[women_graduates_less_25]', '$_POST[women_graduates_more_25]',    																							 		 '$_POST[other_sessions_graduates]','$_POST[other_county]', '$_POST[observations]','$mail_address')");
	
		echo "<br />";	
		echo "<center><h2><font color=\"#00CC00\">Detaliile sesiunii au fost adaugate cu succes ! </font></h2></center>";	
		echo "<br />";	
	}
	else if ( isset($_GET['edit_sesiune']) or isset($_POST['edit_sesiune']) )
	{
		$update_request = mysqli_query($dbc,"UPDATE `sesiuni` SET `data_sesiunii`='$_POST[session_date]',`nr_persoane_inscrise`= '$_POST[session_subscriber_nr]',`nr_persoane_finalizat`= '$_POST[session_finalists]',`nr_participanti_absolvire`='$_POST[exam_participants]', `nr_absolventi_barbati_sub_25`='$_POST[men_graduates_less_25]',`nr_absolventi_barbati_peste_25`='$_POST[men_graduates_more_25]', `nr_asolventi_femei_sub_25`='$_POST[women_graduates_less_25]',`nr_absolventi_femei_peste_25`='$_POST[women_graduates_more_25]',`nr_absolventi_alte_sesiuni`='$_POST[other_sessions_graduates]',`desfasurat_in_alt_judet`='$_POST[other_county]',`observatii`='$_POST[observations]', `state_anc`='neaprobat_anc' WHERE `id_sesiune`='$var_id_sesiune'");
					
		if($update_request)
		{
			echo "<br />";	
			echo "<center><h2><font color=\"#00CC00\">Datele au fost modificate cu succes ! </font></h2></center>";	
			echo "<br />";
		}
		else
		{
			echo "<br />";	
			echo "<center><h2><font color=\"#FF0000\">Datele nu au fost modificate ! Reincercati </font></h2></center>";	
			echo "<br />";
			echo $get_form;
		}
	}	
}
}
				
?> 